package com.tg.sieve_data.config;

import com.tg.sieve_data.interceptor.OAuth2Interceptor;
import com.tg.sieve_data.mapper.sys.SysUserTokenMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class CorsConfig implements WebMvcConfigurer  {
    @Resource
    private SysUserTokenMapper sysUserTokenMapper;
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**") // 允许所有路径
//                        .allowedOrigins("http://172.16.253.247:8080","http://172.16.253.193:8080", "http://localhost:8080") // 允许的前端源地址
                        .allowedOriginPatterns("http://*", "https://*") // 允许的前端源地址
                        .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") // 允许的请求方法
                        .allowedHeaders("*") // 允许所有请求头
                        .allowCredentials(true); // 允许发送认证信息（如 cookie）
            }
        };
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new OAuth2Interceptor(sysUserTokenMapper))
                .addPathPatterns("/**")
                .excludePathPatterns("/test/**", "/activity/**", "/banner/**", "/sys/login",
                        "/v3/api-docs", "/v3/api-docs/**", "/swagger-ui/**", "/swagger-ui.html",
                        "/swagger-ui/index.html", "/webjars/**", "/swagger-resources", "/swagger-resources/**", "/captcha.jpg");
    }

} 